package com.spddr.pmos.cxf.tree.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Service;

import com.spddr.pmos.cxf.tree.JaxrsTradeResultOrgTree;
import com.spddr.pmos.domain.TreeLevel;
import com.spddr.pmos.jpa.Company;

import cn.seqdata.pojo.TreeNode;

@Service
public class JaxrsTradeResultOrgTreeImpl extends AbstractTreeImpl implements JaxrsTradeResultOrgTree{


	@Override
	public Collection<TreeNode> byRoot() {
		List<TreeNode> nodes = new ArrayList<>();
		Company company = companyRepos.findOne(1000000L);
		nodes.addAll(byJpaObject(true, Arrays.asList(company)));
		return nodes;
	}

	@Override
	public Collection<TreeNode> byLevel(long id, TreeLevel level) {
		if (0L == id)
			return byRoot();

		switch (level) {
		case Grid:
			return byGrid(id);
		case Company:
			return byCompany(id);
		case Customer:
			return byCustomer(id);
		case Plant:
			return byPlant(id);
		case Unit:
			return byUnit(id);
		case Substation:
			return bySubstation(id);
		case Transformer:
			return byTransformer(id);
		case Line:
			return byLine(id);
		case Transmission:
			return byTransmission(id);
		case River:
			return byRiver(id);
		case Reservoir:
			return byReservoir(id);
		case UsageGroup:
		case UsagePoint:
			return byUsagePoint(id);
		case Telemetry:
		case Hydrology:
		case Weather:
		case Rain:
			return byTelemetry(id);
		default:
			return Collections.emptyList();
		}
	}
	
	@Override
	public Collection<TreeNode> byCompany(long company) {
		List<TreeNode> nodes = new ArrayList<>();
		List<Company> listcoCompanies = companyRepos.findByParentId(company);
		List<Company> pudianPlant = new ArrayList<Company>();
		List<Company> otherPlant = new ArrayList<Company>();
		for (Company company2 : listcoCompanies) {
			if(company2.getId() == 10000001l){
				pudianPlant.add(company2);
			}else{
				otherPlant.add(company2);
			}
		}
		//结算时：其他的电厂是在电厂结算
		nodes.addAll(byJpaObject(false, otherPlant));
		//结算时：瀑电的电厂是在电站（瀑布沟、深溪沟）结算
		nodes.addAll(byJpaObject(true, pudianPlant));
		nodes.addAll(byJpaObject(false, plantRepos.findByCompanyId(company)));
		return nodes;
	}

	
}
